Skip to content

Add Mooncake.jl extension#1343

Closed
yebai wants to merge 1 commit intoJuliaArrays:masterfrom
yebai:add-mooncake-extension
Closed

Add Mooncake.jl extension#1343
yebai wants to merge 1 commit intoJuliaArrays:masterfrom
yebai:add-mooncake-extension

Conversation

@yebai
Copy link
Copy Markdown

@yebai yebai commented Apr 28, 2026

  • Add StaticArraysMooncakeExt package extension porting Mooncake.jl's existing MooncakeStaticArraysExt. The extension teaches Mooncake that SArray{S,T,N,L} with IEEE float / complex IEEE float element types is a leaf-like differentiable container (its tangent type is itself), and adds frule!! / rrule!! rules for Mooncake._new_ (which IR normalisation lowers SArray(...) constructor calls to) and getindex(::SArray, ::Int).
  • Tests live under test/ext/mooncake/ with their own Project.toml and a self-activating driver script, mirroring how Mooncake's own repo organises ext tests. A new extra CI job (matrix-templated for future ext labels) runs them on Julia 1.10 and the current stable.

🤖 Generated with Claude Code

Port `MooncakeStaticArraysExt` from Mooncake.jl into StaticArrays.jl
as a package extension `StaticArraysMooncakeExt`. The extension teaches
Mooncake that `SArray{S,T,N,L}` with IEEE float / complex IEEE float
element types is a leaf-like differentiable container (its tangent
type is itself), and adds `frule!!` / `rrule!!` rules for `_new_` and
`getindex(::SArray, ::Int)`.

Tests live under `test/ext/mooncake/` with their own `Project.toml`
and a self-activating driver script, and run in a new `extra` CI job
(matrix-templated for future ext labels) on Julia 1.10 and current
stable.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@yebai
Copy link
Copy Markdown
Author

yebai commented Apr 28, 2026

@ChrisRackauckas, this should fix SciML/SciMLSensitivity.jl#1425

@vchuravy
Copy link
Copy Markdown
Contributor

Speaking from my experience maintaining Enzyme.jl, I would encourage this to be an extension in Mooncake for StaticArrays.jl

StaticArrays has been very stable for a while, while the AD landscape is rapidly evolving. Looking back at the Enzyme extension in KernelAbstraction, it has caused the majority of patch version releases in the 0.9 series.

Furthermore, the extension uses Mooncake internals quite a lot and barely requires special knowledge about StaticArrays, so it would make sense if this code lives where Mooncake experts can have an eye on it and there is less risk of it becoming stale.

@vchuravy
Copy link
Copy Markdown
Contributor

Unfortunately, it has to be here or closed.

Why can it not be an extension in Mooncake? Extensions are perfectly symmetrical.

@yebai
Copy link
Copy Markdown
Author

yebai commented Apr 28, 2026

Mooncake's policy means it covers Julia Base only. Existing pkg extensions maybe moved out in the future.

Unfortunately I won't have time to read further comments. I'll fix review comments next week if any. Please feel free to close this if it is out of scope.

@vchuravy
Copy link
Copy Markdown
Contributor

Mooncake's policy means it covers Julia Base only. Existing pkg extensions maybe moved out in the future.

That is your prerogative, but I would ask you to reconsider. A PR and an extension to another package is an implicit ask: "Please take on the maintenance of this piece of code." This is not a small ask, and the internals of an AD package are complicated. I have co-maintained Enzyme.jl, ForwardDiff.jl and even helped with Mooncake and it's interaction with the Julia compiler, yet when I look at this code, I am not confident that I could maintain it.

When deciding where a package extension lives I try to look at the amount of internals of each package it is using. The current extension is mostly Mooncake and very little StaticArrays, so it would very naturally for me be a Mooncake extension.

@wsmoses
Copy link
Copy Markdown

wsmoses commented Apr 29, 2026

I believe this was already discussed and considered by @gdalle in #1342

"""
Closing this PR for now since it is unclear to me that augmenting StaticArrays this way is the right design choice. This code should live in Mooncake
"""

Comment on lines +49 to +65
@foldable function tangent_type(::Type{SArray{S,T,N,L}}) where {S,T<:_SElt,N,L}
return SArray{S,T,N,L}
end

@foldable function tangent_type(
::Type{NoFData}, ::Type{SArray{S,T,N,L}}
) where {S,T<:_SElt,N,L}
return SArray{S,T,N,L}
end

# Non-parametric alias used as a constraint, analogous to `CF` in complex.jl.
const _SAFloat = SArray{S,T,N,L} where {S,T<:_SElt,N,L}

@foldable fdata_type(::Type{T}) where {T<:_SAFloat} = NoFData
@foldable rdata_type(::Type{T}) where {T<:_SAFloat} = T

tangent(::NoFData, t::_SAFloat) = t
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from whether this should live in Mooncake or StaticArrays, was this even reviewed by a human before submission? These definitions look like they should be exactly identical to defaults.

Is this really the kind of pull requests you would welcome in your projects?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is actually needed -- the default tangent for Mooncake is not StaticArrays. Mooncake's automatically derived tangents are usually a NamedTuple of all field members of structs.

The tests for this PR run successfully locally, and I reviewed the code myself before creating it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's all necessary, then it's not much better. Nearly duplicating so much code for each array-like type is going to cause huge maintenance issues. Maybe it could all be generated by a macro that lives in Mooncake.jl?

Anyway, if you don't have the time to address issues raised in a review, making PRs is just a waste of time.

Copy link
Copy Markdown
Author

@yebai yebai Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's all necessary, then it's not much better. Nearly duplicating so much code for each array-like type is going to cause huge maintenance issues. Maybe it could all be generated by a macro that lives in Mooncake.jl?

Will Tebbutt primarily designed Moocanke’s tangent-type system while working with me. Automating this via metaprogramming is difficult, as subtypes of AbstractArray can vary considerably. This reflects lessons learned from Zygote, ChainRules, and other autograd systems. Will has nearly a decade of experience with automatic differentiation in Julia, so I trust his judgment on this.

if you don't have the time to address issues raised in a review, making PRs is just a waste of time.

The level of disagreement has made it difficult to sustain focused technical discussion.

Related: https://chalk-lab.github.io/Mooncake.jl/stable/developer_documentation/custom_tangent_type/#Writing-Custom-Tangent-Types

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automating this via metaprogramming is difficult, as subtypes of AbstractArray can vary considerably.

I have about 8 years of experience writing and maintaining Julia code, and in my opinion automation via metaprogramming would be the easier approach in the long run. This extension is its current form breaks major good engineering practices, for example not accessing internal functions of other packages and only using public API. Unless "internal" in function name is only decorative and they are not internal at all?

@yebai yebai closed this Apr 29, 2026
@yebai yebai deleted the add-mooncake-extension branch April 29, 2026 08:16
@yebai
Copy link
Copy Markdown
Author

yebai commented Apr 29, 2026

I'm closing this PR because I won't have time to continue discussions. The code here can serve as a template for anyone who wants to use StaticArrays with Mooncake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants